package com.common.UITags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * 
 * 类描述 文本录入框
 * @author 张龙
 * @version 1.0
 * 创建时间 2015-4-2
 */
public class TextInputTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	private String id;//id
	private String name;//name
	private String label;//标签前文本
	private String defaultvalue;//默认文本
	private String password = "false";//是否是密码框
	private String dataOptions;//数据验证规则
	private String hidden = "false";
	private String readOnly = "false";
	private String placeHolder;
	private String onChange;
	
	@Override
	public int doStartTag() throws JspException {
		try{
			JspWriter out = pageContext.getOut();
			StringBuffer sBF = new StringBuffer();
			StringBuffer sBFP = new StringBuffer();
			StringBuffer sBFL = new StringBuffer();
			sBFL.setLength(0);
			if(!(label==null||"".equals(label))){
				sBFL.append("<span style=\"font-size: small;vertical-align: middle;font-weight: bold;color:#535C5E;display:inline-block;width:43%;text-align:right;\">" +label);
//				//如果为必录项则加上一个红色的星号
//				if(required!=null&&!"".equals(required)&&"true".equals(required)){
//					sBFL.append("<font color=\"red\">*</font>");
//				}
				sBFL.append(":&nbsp;&nbsp;</span>");
			}
			sBF.setLength(0);
			sBF.append("<input  ");
			//不是隐藏的就用easy-textbox样式，隐藏就不用，easyui的bug
			if (!"true".equals(this.hidden)) {
				sBF.append(" class=\"easyui-textbox ");
				if(dataOptions!=null&&!"".equals(dataOptions)){
					sBF.append(" easyui-validatebox");
				}
				sBF.append("\" ");
				if(dataOptions==null){
					this.dataOptions = "";
				}
				sBF.append(" data-options=\"");
				if(this.placeHolder!=null && !"".equals(this.placeHolder)){
					sBF.append("prompt:'"+this.placeHolder+"',");
				}
				if(this.onChange!=null&&!"".equals(this.onChange)){
					sBF.append("onChange:"+this.onChange+",");
				}
				sBF.append(this.dataOptions+"\" ");
			}
			if("true".equals(password)){
				sBF.append(" type = \"password\" ");
			}
			if(!(id==null ||"".equals(id))){
				sBF.append(" id = \""+id+"\" ");
			}
			if(!(name==null ||"".equals(name))){
				sBF.append(" name = \""+name+"\" ");
			}else{
				sBF.append(" name = \""+id+"\" ");
			}
			if(readOnly.equals("true")){
				sBF.append(" readonly=\"readonly\" ");
			}
			if(!(defaultvalue==null || "".equals(defaultvalue))){
				sBF.append(" value = \""+defaultvalue+"\" ");
			}
			sBF.append("style=\"");
			if ("true".equals(this.hidden)) {
				sBF.append(" display:none; ");
			}
			//为了好看设置宽度为表格宽度的55%
			if(this.getParent() instanceof FormTableTag){
				sBF.append(" width: 100%;");
			}
			sBF.append("text-align: center; ");
			sBF.append("\"");
			sBF.append("></input>");
			if(this.getParent() instanceof FormTableTag && this.hidden.equals("false")){
				sBFP.append("<td width=\"33%\" " );//页面宽度的33%
				sBFP.append(">");
				sBFP.append(sBFL);
				sBFP.append("<div style=\"width:55%;display:inline-block;\">");
				sBFP.append(sBF);
				sBFP.append("</div>");
				sBFP.append("</td>");
				out.print(sBFP);
			}else{
				out.print(sBF.toString());
			}
		}catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		return SKIP_BODY;
	}
	
	
	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}


	
	
	//get and set方法
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getDefaultvalue() {
		return defaultvalue;
	}
	public void setDefaultvalue(String defaultvalue) {
		this.defaultvalue = defaultvalue;
	}
//	public String getRequired() {
//		return required;
//	}
//	public void setRequired(String required) {
//		this.required = required;
//	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getDataOptions() {
		return dataOptions;
	}


	public void setDataOptions(String dataOptions) {
		this.dataOptions = dataOptions;
	}


	public String getHidden() {
		return hidden;
	}


	public void setHidden(String hidden) {
		this.hidden = hidden;
	}


	public String getReadOnly() {
		return readOnly;
	}


	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}


	public String getPlaceHolder() {
		return placeHolder;
	}


	public void setPlaceHolder(String placeHolder) {
		this.placeHolder = placeHolder;
	}


	public String getOnChange() {
		return onChange;
	}


	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}
	
}
